package cn.zk.service.impl;

import cn.zk.service.IVerificationCodeService;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.SpecCaptcha;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class VerificationCodeServiceImpl implements IVerificationCodeService {

    @Override
    public Map<String, Object> getArithmeticCaptcha() {
        // 算术类型
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        // 几位数运算，默认是两位
        captcha.setLen(3);
        // 获取运算的公式：4-9+1=?
        captcha.getArithmeticString();
        // 获取运算的结果：-4
        String text = captcha.text();
        System.out.println("计算结果为："+text);

        HashMap<String, Object> map = new HashMap<>();
        map.put("uuid", UUID.randomUUID().toString());
        map.put("img",captcha.toBase64());// base64编码输出
        return map;
    }

    @Override
    public Map<String, Object> getSpecCaptcha() {
        // png类型
        SpecCaptcha captcha = new SpecCaptcha(130, 48);
        String text = captcha.text();// 获取验证码的字符
        char[] chars = captcha.textChar();// 获取验证码的字符数组

        System.out.println("验证码："+text);
        System.out.println(chars);

        HashMap<String, Object> map = new HashMap<>();
        map.put("uuid", UUID.randomUUID().toString());
        map.put("img",captcha.toBase64().substring(22));// base64编码输出
        return map;
    }
}
